package com.rem.stream.part1;

import com.rem.lambda.part1.Employee;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * Stream 的中间操作====排序
 *
 * @Author Rem
 * @Date 2020-02-13
 */

public class TestStream4 {

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 16, 3468.12),
            new Employee("李四", 42, 9485.32),
            new Employee("王五", 24, 8482.99),
            new Employee("赵六", 33, 6381.82),
            new Employee("田七", 36, 4448.45),
            new Employee("方八", 36, 7485.42)
    );

    /**
     * sorted()===================自然排序(Comparable)
     * sorted(Comparator com)=====定制排序
     */


    //sorted
    @Test
    public void test1() {
        List<String> list = Arrays.asList("aaa", "fff", "ccc", "eee", "ddd", "bbb");
        list.stream()
                .sorted()
                .forEach(System.out::println);
    }


    //sorted(Comparator com)
    //需求按照年龄倒序排列，年龄一样按照姓名排序
    @Test
    public void test2() {
        employees.stream()
                .sorted((e1, e2) -> {
                    return -e1.getAge().compareTo(e2.getAge());
                   /* if (e1.getAge().equals(e2.getAge())) {
                        return e1.getName().compareTo(e2.getName());
                    } else {
                        return -e1.getAge().compareTo(e2.getAge());
                    }*/
                })
                .forEach(System.out::println);
    }


}
